package main.Link;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

public class SQLLink {

    private String action, taskShortName, taskName, smrName, smrNum, dbName, smNum, unit, pvType, smrUnit;
    private String smrMapId;
    private int taskId, smrId, id, pvTrId, mechTrIdm, tTrId, mrTrId, projectId, asuId;
    private BigDecimal smrVol, fact, factFull, pvVol, mechVol, tVol, mrVol, actualPv;
    private Boolean ask, pv, isImportFactage;

    public SQLLink (int id, String action, String taskShortName, String taskName, String smrNum, String smrName, Double smrVol, Double fact, Double factFull, Boolean pv, int taskId, int smrId, String smNum, Double pvVol, String smrUnit ) {
        this.action = action;
        this.id = id;
        this.ask = true;
        this.taskShortName = taskShortName;
        this.taskName = taskName;
        this.smrNum = smrNum;
        this.smrName = smrName;
        this.smrVol = new BigDecimal(smrVol).setScale(6, RoundingMode.HALF_UP);
        this.fact = new BigDecimal(fact).setScale(6, RoundingMode.HALF_UP);
        this.factFull = new BigDecimal(factFull).setScale(6, RoundingMode.HALF_UP);
        this.pv = pv;
        this.taskId = taskId;
        this.smrId = smrId;
        this.smrMapId = "0-" + smrId;
        this.smNum = smNum;
        this.pvVol = new BigDecimal(pvVol).setScale(6, RoundingMode.HALF_UP);
        this.smrUnit = smrUnit;

    }

    public SQLLink (int id, String action, String taskShortName, String taskName, String smrNum, String smrName, BigDecimal smrVol, BigDecimal fact, BigDecimal factFull, Boolean pv, int taskId, int smrId, String smNum, BigDecimal pvVol, String smrUnit ) {
        this.action = action;
        this.id = id;
        this.ask = true;
        this.taskShortName = taskShortName;
        this.taskName = taskName;
        this.smrNum = smrNum;
        this.smrName = smrName;
        this.smrVol = smrVol;
        this.fact = fact;
        this.factFull = factFull;
        this.pv = pv;
        this.taskId = taskId;
        this.smrId = smrId;
        this.smrMapId = "0-" + smrId;
        this.smNum = smNum;
        this.pvVol = pvVol;
        this.smrUnit = smrUnit;
    }

    public SQLLink() {

    }

    public void updateSQL (Connection connSql) {
        try {
            CallableStatement stmt;
            stmt = connSql.prepareCall("{call updateLink(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}");
            stmt.setString(1, dbName);
            stmt.setInt(2, projectId);
            stmt.setInt(3, taskId);
            stmt.setInt(4, smrId);
            stmt.setString(5, smrName);
            stmt.setString(6, smrNum);
            stmt.setString(7, smNum);
            stmt.setFloat(8, smrVol.floatValue());
            stmt.setFloat(9, fact.floatValue());
            stmt.setFloat(10, factFull.floatValue());
            stmt.setFloat(11, pvVol.floatValue());
            stmt.setFloat(12, mechVol.floatValue());
            stmt.setFloat(13, tVol.floatValue());
            stmt.setFloat(14, mrVol.floatValue());
            stmt.setString(15, pv.toString());
            stmt.registerOutParameter(16, Types.INTEGER);

            stmt.executeUpdate();

            int ret = stmt.getInt(16);

            if ( ret != 0 )  this.id = ret;

            stmt.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public String getAction() { return action; }

    public void setAction(String action) { this.action = action; }

    public String getTaskShortName() { return taskShortName; }

    public void setTaskShortName(String taskShortName) { this.taskShortName = taskShortName; }

    public String getTaskName() { return taskName; }

    public void setTaskName(String taskName) { this.taskName = taskName; }

    public String getSmrName() { return smrName; }

    public void setSmrName(String smrName) { this.smrName = smrName; }

    public int getSmrId() { return smrId; }

    public void setSmrId(int smrId) { this.smrId = smrId; }

    public int getTaskId() { return taskId; }

    public void setTaskId(int taskId) { this.taskId = taskId; }

    public String getSmrNum() { return smrNum; }

    public void setSmrNum(String smrNum) { this.smrNum = smrNum; }

    public BigDecimal getSmrVol() { return smrVol; }

    public void setSmrVol(BigDecimal smrVol) { this.smrVol = smrVol; }

    public BigDecimal getFact() { return fact; }

    public void setFact(BigDecimal fact) { this.fact = fact; }

    public BigDecimal getFactFull() { return factFull; }

    public void setFactFull(BigDecimal factFull) { this.factFull = factFull; }

    public Boolean getPv() { return pv; }

    public void setPv(Boolean pv) { this.pv = pv; }

    public Boolean getAsk() { return ask; }

    public void setAsk(Boolean ask) { this.ask = ask; }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPvTrId() { return pvTrId; }

    public void setPvTrId(int pvTrId) { this.pvTrId = pvTrId; }

    public BigDecimal getPvVol() { return pvVol; }

    public void setPvVol(BigDecimal pvVol) { this.pvVol = pvVol; }

    public int getMechTrIdm() { return mechTrIdm; }

    public void setMechTrIdm(int mechTrIdm) { this.mechTrIdm = mechTrIdm; }

    public BigDecimal getMechVol() { return mechVol; }

    public void setMechVol(BigDecimal mechVol) { this.mechVol = mechVol; }

    public int getTTrId() { return tTrId; }

    public void setTTrId(int tTrId) { this.tTrId = tTrId; }

    public BigDecimal getTVol() { return tVol; }

    public void setTVol(BigDecimal tVol) { this.tVol = tVol; }

    public int getMrTrId() { return mrTrId; }

    public void setMrTrId(int mrTrId) { this.mrTrId = mrTrId; }

    public BigDecimal getMrVol() { return mrVol; }

    public void setMrVol(BigDecimal mrVol) { this.mrVol = mrVol; }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public void setSmNum(String smNum) {
        this.smNum = smNum;
    }

    public String getSmNum() {
        return smNum;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getPvType() {
        return pvType;
    }

    public void setPvType(String pvType) {
        this.pvType = pvType;
    }

    public String getSmrUnit() {
        return smrUnit;
    }

    public void setSmrUnit(String smrUnit) {
        this.smrUnit = smrUnit;
    }

    public BigDecimal getActualPv() {
        return actualPv;
    }

    public void setActualPv(BigDecimal actualPv) {
        this.actualPv = actualPv;
    }

    public int getAsuId() {
        return asuId;
    }

    public void setAsuId(int asuId) {
        this.asuId = asuId;
    }

    public Boolean getIsImportFactage() {
        return isImportFactage;
    }

    public void setIsImportFactage(Boolean isImportFactage) {
        this.isImportFactage = isImportFactage;
        this.smrMapId = (isImportFactage ? 1 : 0) + "-" + smrId;
    }

    //region applyUnitMultiplier(BigDecimal curPvVol) - Назначение коэффициента со строки ед изм расценки
    public void applyUnitMultiplier(BigDecimal curPvVol) {
        int unitMultiplier = 1;
        unit = smrUnit;
        if (smrUnit != null && smrUnit.length() > 3) {
            String sup = smrUnit.substring(0,4);
            if (sup.contains("1000")) {
                unitMultiplier = 1000;
                unit = smrUnit.replace("1000", "");
            } else if (sup.contains("100")) {
                unitMultiplier = 100;
                unit = smrUnit.replace("100", "");
            } else if (sup.contains("10")) {
                unitMultiplier = 10;
                unit = smrUnit.replace("10", "");
            }
        }

        pvVol = curPvVol.multiply(new BigDecimal(unitMultiplier)).setScale(6, RoundingMode.HALF_UP);
        //smrVol = smrVol.multiply(new BigDecimal(unitMultiplier)).setScale(6, RoundingMode.HALF_UP);
    }

    public void calcUnit(BigDecimal curPvVol) {
        unit = smrUnit;
        if (smrUnit != null && smrUnit.length() > 3) {
            String sup = smrUnit.substring(0,4);
            if (sup.contains("1000")) {
                unit = smrUnit.replace("1000", "");
            } else if (sup.contains("100")) {
                unit = smrUnit.replace("100", "");
            } else if (sup.contains("10")) {
                unit = smrUnit.replace("10", "");
            }
        }
    }

    public String getSmrMapId() {
        return smrMapId;
    }

    //endregion

}
